package com.linkstec.musiccloud.service.impl;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkstec.musiccloud.mapper.SongMapper;
import com.linkstec.musiccloud.pojo.ListSongUser;
import com.linkstec.musiccloud.pojo.ListenTime;
import com.linkstec.musiccloud.pojo.Song;
import com.linkstec.musiccloud.service.SongService;
import com.linkstec.musiccloud.util.Page;
import com.linkstec.musiccloud.util.ToolUtil;
import com.linkstec.musiccloud.vo.SongVo;

import com.linkstec.musiccloud.service.SonglistService;

import com.linkstec.musiccloud.vo.SongVos;
@Service
public class SongServiceImpl implements SongService{
	
	@Autowired
	private SongMapper songMapper;
	
	@Autowired
	private SonglistService songlistService;

	@Override
	public List<Song> selectSongByType(int type) {
		return songMapper.selectSongByType(type);
	}

	@Override
	public Song selectSongById(int songId) {
		songMapper.addSongCounts(songId);
		return songMapper.selectSongById(songId);
	}
		
	public List<Song> selectSong(Song songName) {
		return songMapper.selectSong(songName);
	}

	@Override
	public List<SongVos> selectSongByList(ListSongUser listSongUser) {
		songlistService.addListCounts(listSongUser);
		return songMapper.selectSongByList(listSongUser);
	}

	@Override
	public List<SongVo> selectAllSong(Page page) throws RuntimeException {
		int state = 1;
		page.setCurPage((page.getCurPage()-1)*page.getPageNumber());
		return songMapper.selectAllSong(page);
	}

	public List<Song> selectSongByCounts() {
		return songMapper.selectSongByCounts();
	}

	@Override
	public List<Song> selectSongByCreate() {
		return songMapper.selectSongByCreate();

	}

	@Override
	public void mSongAdd(Song song) throws RuntimeException {
		song.setCreateTime(ToolUtil.getCurrentTime());
		song.setSongCounts(0);
		song.setState(1);
		songMapper.mSongAdd(song);
		
	}

	@Override
	public List<Song> selectAllSongCounts() {
		int counts = songMapper.selectAllSongCounts();//获取总歌数
		Random ra = new Random();
		int[] arr = new int[10];//定义一个数组来存放随机的十个数
		for (int i = 0; i < arr.length; i++) {
			int num = ra.nextInt(counts)+1;
			for (int j = 0; j < arr.length; j++) {
				if (num==arr[j]) {
					i--;
					break;
				}else {
					arr[i] = num;
					break;
				}
			}
		}
		return songMapper.selectSongByRandom(arr);
		
	}

	@Override
	public List<Song> selectSongByComment() {
		return songMapper.selectSongByComment();
	}

	@Override
	public void deleteSongs(Song song) throws RuntimeException {
		songMapper.deleteSongs(song);
		
	}
	
	@Override
	public void mSongUpdate(Song song) throws RuntimeException {
		songMapper.mSongUpdate(song);
		
	}

	@Override
	public List<SongVo> selectSongsBySinger(String search) throws RuntimeException {
		return songMapper.selectSongsBySinger(search);
	}

	@Override
	public List<SongVo> selectSongsByName(String search) throws RuntimeException {
		return songMapper.selectSongsByName(search);
	}

	@Override
	public List<Song> selectSinger(String singer) {
		return songMapper.selectSinger(singer);
	}

	@Override
	public void insertSong(Song song) {
		songMapper.insertSong(song);
	}
		
	public List<SongVo> selectVatSongs() throws RuntimeException {
		int state = 0; 
		return songMapper.selectVatSongs(state);
	}

	@Override
	public void submitSongs(int[] ids) throws RuntimeException {
		songMapper.submitSongs(ids);
		
	}


	@Override
	public List<SongVo> getAllSongs() throws RuntimeException {
		return songMapper.getAllSongs();
		
	}

	@Override
	public SongVo getSongVoById(SongVo song) throws RuntimeException {
		return songMapper.getSongVoById(song);
	}
	
	@Override
	public void ltime(ListenTime ltime) {
		int time = 0;
		time = songMapper.selectTime(ltime);
		if (time != 0) {
			songMapper.upTime(ltime);
		} else {
			songMapper.addTime(ltime);
		}
	}

	@Override
	public List<Song> selectHistory(int userId) {
		return songMapper.selectHistory(userId);
	}
	


}
